﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using DataAccessLayer;
using DTO;
using System.Data;

namespace DAL
{
    public class NguoiNhanHangDAL
    {
        private DBManager _dbmanager;

        #region Phương thức khởi tạo mặc định
        public NguoiNhanHangDAL()
        {
            string _connectionInfo = ConfigurationManager.AppSettings["ConnectionInfo"];
            _dbmanager = new DBManager(DataProvider.SqlServer, _connectionInfo);
        }
        #endregion

        #region Phương thức tạo Object NguoiNhanHangDTO từ IDataReader
        public NguoiNhanHangDTO CreateNguoiNhanHangFromReader(IDataReader reader)
        {
            NguoiNhanHangDTO _khDTO = new NguoiNhanHangDTO();

            _khDTO.MaNguoiNhanHang = (int)reader["MaNguoiNhanHang"];
            _khDTO.MaKhachHang = reader["MaKhachHang"].ToString();
            _khDTO.TenNguoiNhanHang = reader["TenNguoiNhanHang"].ToString();
            _khDTO.GioiTinh = Convert.ToSByte(reader["GioiTinh"]);
            _khDTO.DiaChi = reader["DiaChi"].ToString();
            _khDTO.CMND = reader["CMND"].ToString();            
            _khDTO.DienThoai = reader["DienThoai"].ToString();

            return _khDTO;
        }
        #endregion

        #region Lấy danh sách người nhận của 1 khách hàng

        public List<NguoiNhanHangDTO> usp_GetListNguoiNhanHangTheoMaKhachHang(string makh)
        {
            List<NguoiNhanHangDTO> _lstNNH = new List<NguoiNhanHangDTO>();
            IDataReader _reader = null;
            try
            {
                _dbmanager.Open();
                _dbmanager.CreateParameters(1);
                _dbmanager.AddParameters(0, "@ma_kh", makh);
                _reader = _dbmanager.ExecuteReader(CommandType.StoredProcedure, "usp_getListNguoiNhanHangTheoMaKhachHang");
                while (_reader.Read())
                {
                    NguoiNhanHangDTO nnhDTO = new NguoiNhanHangDTO();
                    nnhDTO = this.CreateNguoiNhanHangFromReader(_reader);
                    _lstNNH.Add(nnhDTO);
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                if (_reader != null)
                    _reader.Close();
                _dbmanager.Close();
            }

            return _lstNNH;
        }
        #endregion

        #region Lấy danh sách người nhận hàng

        public List<NguoiNhanHangDTO> GetListNguoiNhanHang()
        {
            List<NguoiNhanHangDTO> _lstNNH = new List<NguoiNhanHangDTO>();
            IDataReader _reader = null;
            try
            {
                _dbmanager.Open();
                _reader = _dbmanager.ExecuteReader(CommandType.StoredProcedure, "usp_GetListNguoiNhanHang");
                while (_reader.Read())
                {
                    NguoiNhanHangDTO nnhDTO = new NguoiNhanHangDTO();
                    nnhDTO = this.CreateNguoiNhanHangFromReader(_reader);
                    _lstNNH.Add(nnhDTO);
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                if (_reader != null)
                    _reader.Close();
                _dbmanager.Close();
            }

            return _lstNNH;
        }
        #endregion

        #region Lấy địa chỉ người nhận
        public NguoiNhanHangDTO GetNguoiNhanHang(string manguoinhan)
        {
            NguoiNhanHangDTO _nnhDTO = new NguoiNhanHangDTO();
            IDataReader _reader = null;
            try
            {
                _dbmanager.Open();
                _dbmanager.CreateParameters(1);
                _dbmanager.AddParameters(0, "@ma_nnh", manguoinhan);
                _reader = _dbmanager.ExecuteReader(CommandType.StoredProcedure, "usp_GetNguoiNhanHang");
                while (_reader.Read())
                    _nnhDTO = this.CreateNguoiNhanHangFromReader(_reader);
                
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                if (_reader != null)
                    _reader.Close();
                _dbmanager.Close();
            }

            return _nnhDTO;
        }

        #endregion

        #region Thêm người nhận hàng
        public int themNguoiNhanHang(NguoiNhanHangDTO nnhDTO)
        {
            int _index = 0;
            try
            {
                _dbmanager.Open();
                _dbmanager.CreateParameters(6);                
                _dbmanager.AddParameters(0, "@ma_kh", nnhDTO.MaKhachHang);
                _dbmanager.AddParameters(1, "@ten_nnh", nnhDTO.TenNguoiNhanHang);
                _dbmanager.AddParameters(2, "@gioitinh", nnhDTO.GioiTinh);
                _dbmanager.AddParameters(3, "@diachi", nnhDTO.DiaChi);
                _dbmanager.AddParameters(4, "@cmnd", nnhDTO.CMND);
                _dbmanager.AddParameters(5, "@dienthoai", nnhDTO.DienThoai);
                
                //_dbmanager.Parameters[0].Direction = ParameterDirection.Output;
                _index = _dbmanager.ExecuteNonQuery(CommandType.StoredProcedure, "usp_ThemNguoiNhanHang");

                //manguoinhanhang = Convert.ToInt32(_dbmanager.Parameters[0].Value);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                _dbmanager.Close();
            }
            return _index;
        }
        #endregion
    }
}